package com.sdweizan.sdk.bytedance.api.impl;

import com.alibaba.fastjson.TypeReference;
import com.sdweizan.sdk.bytedance.api.BdApiService;
import com.sdweizan.sdk.bytedance.api.BdMiniAppService;
import com.sdweizan.sdk.bytedance.bean.login.BdCode2SessionRequest;
import com.sdweizan.sdk.bytedance.bean.login.BdCode2SessionResponse;
import com.sdweizan.sdk.bytedance.config.BdConfigStorage;
import com.sdweizan.sdk.bytedance.error.BdErrorException;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class BdLoginService {
  private final BdApiService bdApiService;

  public BdCode2SessionResponse code2Session(String code, String anonymousCode)
      throws BdErrorException {
    BdConfigStorage configStorage = bdApiService.getBdMiniAppService().getConfigStorage();
    BdCode2SessionRequest bdCode2SessionRequest =
        BdCode2SessionRequest.builder()
            .code(code)
            .anonymousCode(anonymousCode)
            .appid(configStorage.getAppId())
            .secret(configStorage.getSecret())
            .build();
    return bdApiService.post(bdCode2SessionRequest, new TypeReference<BdCode2SessionResponse>() {});
  }
}
